#include <windows.h>
#include "MySQL/mysql.h"

#include "CConsole.h"
#include "CDb.h"
#include "CItem.h"
#include "CBasicPacket.h"

#include <string>
using namespace std;

CItem::CItem() {
	// Item 0xFFFF = "nothing"
	m_id = 0xFFFF;

	m_quantity = 0;
	m_stackSize = 1;

	// Item = normal item (not enchanted, augmented)
	m_type = ITEM_NORMAL;
	// Item = general inventory type item (not crystal, key, armor, weapon)
	m_invType = ITEM_GENERAL;

	memset(m_name, 0, 20);

	m_sellPrice = 0;

	m_message = 0;
	m_animation = 0;

	m_paramtwo = 0;
}

CItem::~CItem() {
}

void CItem::setId(unsigned short id) {
	m_id = id;
}

unsigned short CItem::getId() {
	return m_id;
}


void CItem::setType(unsigned char type) {
	m_type = type;
}

unsigned char CItem::getType() {
	return m_type;
}

void CItem::setInvType(unsigned char type) {
	m_invType = type;
}

unsigned char CItem::getInvType() {
	return m_invType;
}

void CItem::setQuantity(unsigned long quantity) {
	m_quantity = quantity;
}

unsigned long CItem::getQuantity(void) {
	return m_quantity;
}

void CItem::setStackSize(unsigned char stackSize) {
	m_stackSize = stackSize;
}

unsigned char CItem::getStackSize() {
	return m_stackSize;
}


char * CItem::getSignName(void) {
	return m_signName;
}

char * CItem::getSignNameComp(void) {
	return m_signNameComp;
}

void CItem::setSignName(unsigned char * name) {
	memcpy(m_signName, 0, 16);
	memcpy(m_signName, name, sizeof(name));
	memcpy(m_signNameComp, 0, 12);
	compressSignName(m_signName, m_signNameComp);
}

void CItem::compressSignName(char * inputUncomp, char * outputComp)
{
	char tempChar;
	for(unsigned short currChar=0;currChar<=strlen(inputUncomp);currChar++)
	{
		tempChar = 0x00;
		if ((inputUncomp[currChar]>='0') && (inputUncomp[currChar]<='9'))
			tempChar=m_signName[currChar]-'0'+1;
		else if ((inputUncomp[currChar]>='A') && (inputUncomp[currChar]<='Z'))
			tempChar=inputUncomp[currChar]-'A'+11;
		else if ((inputUncomp[currChar]>='a') && (inputUncomp[currChar]<='z'))
			tempChar=inputUncomp[currChar]-'a'+37;
		CBasicPacket::packBitsLE((unsigned char*)outputComp,(unsigned int)tempChar,0,6*currChar,6);
	}
}

void CItem::setSellPrice(unsigned long sellPrice) {
	m_sellPrice = sellPrice;
}

unsigned long CItem::getSellPrice() {
	return m_sellPrice;
}

void CItem::setMessage(unsigned short message) {
	m_message = message;
}

void CItem::setAnimation(unsigned short animation) {
	m_animation = animation;
}

void CItem::setAttributes() {
	
	int count;
	MYSQL_RES * res;
	MYSQL_ROW row;
	char query[4096];

	sprintf(query, "SELECT `Name`, `Activation`, `ValidTargets` FROM stats_usable WHERE ItemID = %i;", this->getId());
	count = (int) strlen(query);
	
	MYSQL* mysqlConn=CDb::getConn(); //get conn
	mysql_real_query(mysqlConn, query, count);

	res = mysql_store_result(mysqlConn);
	CDb::pushConn(mysqlConn); //release conn

	if(!res)
	{
		CConsole::outErr("Error looking up item valid targets!");
		return;
	}
	if(!mysql_num_rows(res)){
		mysql_free_result(res);
	}
	else {
		while( row = mysql_fetch_row(res) ) {
			memcpy(&m_name,row[0],strlen(row[0]));
			m_delay = atoi(row[1]);
			m_allowedTargets = atoi(row[2]);
		}
		mysql_free_result(res);
	}
	m_canRemove = true;
}

unsigned short CItem::getAllowedTargets() {
	return m_allowedTargets;
}

unsigned short CItem::getDelay() {
	return m_delay;
}

char * CItem::getItemName(void) {
	return m_name;
}